package com.quanyan.stadiumScheduler.service.impl;

import com.quanyan.stadiumScheduler.constants.GameConstants;
import com.quanyan.stadiumScheduler.entity.TbGameGradeIntegral;
import com.quanyan.stadiumScheduler.entity.TbGameGradeIntegralExample;
import com.quanyan.stadiumScheduler.entity.TbUserIntegral;
import com.quanyan.stadiumScheduler.entity.TbUserIntegralExample;
import com.quanyan.stadiumScheduler.entity.vo.UserGradeIntegral;
import com.quanyan.stadiumScheduler.mapper.TbGameGradeIntegralMapper;
import com.quanyan.stadiumScheduler.mapper.TbUserIntegralMapper;
import com.quanyan.stadiumScheduler.service.GradeIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author lds
 * @date 2017/2/14 17:10
 */

@Service
public class GradeIntegralServiceImpl implements GradeIntegralService{

    @Autowired
    private TbUserIntegralMapper tbUserIntegralMapper;

    @Autowired
    private TbGameGradeIntegralMapper tbGameGradeIntegralMapper;

    @Override
    public UserGradeIntegral getUserGradeInfo(Integer uid, Integer categoryId) {
        UserGradeIntegral userGradeIntegral = new UserGradeIntegral();
        TbUserIntegralExample example = new TbUserIntegralExample();
        TbUserIntegralExample.Criteria criteria = example.createCriteria();
        criteria.andGameCategoryIdEqualTo(categoryId);
        criteria.andUserIdEqualTo(uid);
        List<TbUserIntegral> userIntegralList = tbUserIntegralMapper.selectByExample(example);
        if (null != userIntegralList && userIntegralList.size() > 0){
            TbUserIntegral userIntegral = userIntegralList.get(0);
            userGradeIntegral.setIntegral(userIntegral.getIntegral());
            userGradeIntegral.setLevel(userIntegral.getLevel());
            userGradeIntegral.setGrade(userIntegral.getLevel().toString());
            userGradeIntegral.setGradeIcon(userIntegral.getIcon());
        }else{
            Integer integral = GameConstants.GRADE_MIN_GRADE_POINT_FOR_SMALLBALL;
            userGradeIntegral = this.getGradeByIntegral(categoryId,integral);
            /**
             * 初始化用户数据
             */
            this.initUserGradeIntegral(uid,categoryId,null);
        }
        return userGradeIntegral;
    }

    public UserGradeIntegral getGradeByIntegral(Integer categoryId,Integer integral){
        /**
         * 判断等级分是否超过系统规定
         * 范围：1000 —— 5999
         */
        Integer minValue = GameConstants.GRADE_MIN_GRADE_POINT_FOR_SMALLBALL;
        Integer maxValue = GameConstants.INTEGRAL_MAXVALUE;
        if(integral < minValue){
            integral = minValue;
        }
        if(integral > maxValue){
            integral = maxValue;
        }
        TbGameGradeIntegralExample tbGameGradeIntegralExample = new TbGameGradeIntegralExample();
        TbGameGradeIntegralExample.Criteria criteria = tbGameGradeIntegralExample.createCriteria();
        criteria.andGameTypeEqualTo(categoryId);
        criteria.andLimitValueLessThanOrEqualTo(integral);
        criteria.andMaxValueGreaterThanOrEqualTo(integral);
        UserGradeIntegral userGradeIntegral = new UserGradeIntegral();
        userGradeIntegral.setIntegral(integral);
        List<TbGameGradeIntegral> gameGradeIntegrals = tbGameGradeIntegralMapper.selectByExample(tbGameGradeIntegralExample);
        if (null != gameGradeIntegrals && gameGradeIntegrals.size() == 1){
            TbGameGradeIntegral gameGradeIntegral = gameGradeIntegrals.get(0);
            userGradeIntegral.setGrade(gameGradeIntegral.getGrade());
            userGradeIntegral.setGradeIcon(gameGradeIntegral.getGradeIcon());
            userGradeIntegral.setLevel(gameGradeIntegral.getLevel().doubleValue());
            userGradeIntegral.setStars(gameGradeIntegral.getStars());
        }
        return userGradeIntegral;
    }

    /**
     * 初始化用户等级积分
     */
    private Integer initUserGradeIntegral(Integer uid, Integer categoryId,Integer integral) {
        if(null == integral){
            integral = GameConstants.GRADE_MIN_GRADE_POINT_FOR_SMALLBALL;
        }
        TbUserIntegral tbUserIntegral = new TbUserIntegral();
        tbUserIntegral.setIntegral(integral);
        tbUserIntegral.setLevel(integral.doubleValue()/1000);
        tbUserIntegral.setGameCategoryId(categoryId);
        tbUserIntegral.setUserId(uid);
        tbUserIntegral.setFlag(GameConstants.NO);
        /**
         * 获取等级分对应图标
         */
        UserGradeIntegral userGradeIntegral = this.getGradeByIntegral(categoryId,integral);
        tbUserIntegral.setIcon(userGradeIntegral.getGradeIcon());
        tbUserIntegralMapper.insertSelective(tbUserIntegral);
        return integral;
    }
}
